﻿using Business;
using Model;
using Public;
using Public.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using wochu.DPS.API.Infrastructure;

namespace wochu.DPS.API.Controllers
{
    public class PFTSaleorderController : ApiController
    {
        private static BPFTSaleorder _NewInstance = new BPFTSaleorder();

        public static BPFTSaleorder NewInstance
        {
            get
            {
                if (_NewInstance == null)
                {
                    _NewInstance = new BPFTSaleorder();
                }

                return _NewInstance;
            }
        }

        /// <summary>
        /// 根据批次号获取订单及巷道信息
        /// </summary>
        /// <param name="BatchNO"></param>
        /// <returns></returns>
        [HttpGet]
        public HttpResponseMessage GetpftSaleOrderLstByBatchNO(string BatchNO)
        {
            MB.Util.TraceEx.Write("调用：GetpftSaleOrderLstByBatchNO");
            PFTSaleorderLocationModel _result = NewInstance.getpftSaleOrderList(BatchNO);
            if (_result != null)
            {
                return ResultsExtensions.GetSuccessJson(_result);
            }
            else
            { 
                SysSetting sysSet = new SysSetting();
                sysSet.Key = Consts.strFalse;
                sysSet.Value = "未获取到摘果订单信息，请检查分拣波次号";
                return ResultsExtensions.GetFailJson(sysSet);
            }
        }

        /// <summary>
        /// 根据批次号和巷道号获取九宫格订单信息（带分拣是否完成标识）0代表需分拣-红色1代表不需要-绿色
        /// </summary>
        /// <param name="BatchNO"></param>
        /// <param name="roadway"></param>
        /// <returns></returns>
        [HttpGet]
        public HttpResponseMessage GetpftSaleOrderLstByBatchNORoadway(string BatchNO,string roadway)
        {
            MB.Util.TraceEx.Write("调用：GetpftSaleOrderLstByBatchNORoadway");
            roadway = roadway.PadLeft(2, '0');
            PFTSaleorderLocationModel _result = NewInstance.getpftSaleOrderList(BatchNO, roadway);
            if (_result != null)
            {
                return ResultsExtensions.GetSuccessJson(_result);
            }
            else
            {
                SysSetting sysSet = new SysSetting();
                sysSet.Key = Consts.strFalse;
                sysSet.Value = "未获取到摘果订单信息，请检查分拣波次号";
                return ResultsExtensions.GetFailJson(sysSet);
            }
        }

        /// <summary>
        ///  根据批次号和巷道号获取“待分拣”订单信息（带分拣是否完成标识）0代表需分拣
        /// </summary>
        /// <param name="BatchNO"></param>
        /// <param name="roadway"></param>
        /// <returns></returns>
        [HttpGet]
        public HttpResponseMessage GetSaleOrderSortingList(string BatchNO, string roadway)
        {
            MB.Util.TraceEx.Write("调用：GetSaleOrderSortingList");
            roadway = roadway.PadLeft(2, '0');
            PFTSaleorderLocationModel _result = NewInstance.getSortingPftSaleOrderList(BatchNO, roadway);
            if (_result != null)
            {
                return ResultsExtensions.GetSuccessJson(_result);
            }
            else
            {
                SysSetting sysSet = new SysSetting();
                sysSet.Key = Consts.strFalse;
                sysSet.Value = "未获取到摘果订单信息，请检查分拣波次号";
                return ResultsExtensions.GetFailJson(sysSet);
            }
        }

        /// <summary>
        /// 绑定手机和分拣批次号
        /// </summary>
        /// <param name="Batch"></param>
        /// <param name="Phone"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage SetBatchPhone(string Batch, string Phone,int UserID)
        {
            MB.Util.TraceEx.Write("调用：SetBatchPhone");
            if (string.IsNullOrEmpty(Batch) || string.IsNullOrEmpty(Phone))
            {
                return ResultsExtensions.GetFailJson(Setting.Instance.getMessageByKey("P0004"));
            }
            int bindState=NewInstance.getSortingflagByBatchNO(Batch,Phone);
            if (bindState == 3)
            {
                return ResultsExtensions.GetFailJson(Setting.Instance.getMessageByKey("P0008")); 
            }
            if (bindState ==1)
            { 
                return ResultsExtensions.GetFailJson(Setting.Instance.getMessageByKey("P0005")); 
            }
            if (bindState == 2)
            {
                return ResultsExtensions.GetSuccessJson(true);
            }
            int SortingFlag=1;//分拣中
            if (NewInstance.bindBatchandPhone(Batch, Phone, UserID, SortingFlag))
            {
                return ResultsExtensions.GetSuccessJson(true);
            }
            else
            {
                return ResultsExtensions.GetFailJson(Setting.Instance.getMessageByKey("P0006"));
            }
        }

        /// <summary>
        /// 设置当前批次的分拣巷道
        /// </summary>
        /// <param name="BatchNO"></param>
        /// <param name="RoadWay"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage setCurrentRoadway(string BatchNO, string RoadWay)
        {
            MB.Util.TraceEx.Write("调用：setCurrentRoadway");
            bool _result = false;
            string ErrorStr = "";
            RoadWay = RoadWay.PadLeft(2, '0');
            _result = NewInstance.SetpftCurrentRoadway(BatchNO, RoadWay, out ErrorStr);//设置当前批次号的巷道号
            if(_result)
            {
                return ResultsExtensions.GetSuccessJson(true);
            }
            else
            {

                return ResultsExtensions.GetFailJson(new SysSetting() { Key = "0", Value = "设置巷道失败:" + ErrorStr });
            }
        }

        /// <summary>
        /// 点亮电子标签
        /// </summary>
        /// <param name="BatchNO"></param>
        /// <param name="RoadWay"></param>
        /// <param name="OrderNO"></param>
        /// <param name="OrderSEQ"></param>
        /// <returns></returns>
        [HttpGet]
        public HttpResponseMessage LightElecLabel(string BatchNO, string RoadWay, string OrderNO)
        {
            MB.Util.TraceEx.Write("调用：LightElecLabel");
            bool _result = false;
            string errorMsg="";
            RoadWay = RoadWay.PadLeft(2, '0');
            //点亮订单标签灯
            _result = NewInstance.LightupElectronicLabel(BatchNO, RoadWay, OrderNO, out errorMsg);
            if (_result)
            {
                return ResultsExtensions.GetSuccessJson("灯已点亮");
            }
            else
            {
                return ResultsExtensions.GetFailJson(new SysSetting() { Key = "0", Value = errorMsg });
            }
        }

        /// <summary>
        /// 强制结束当前批次，当所有明细均分拣时，订单状态设置为已分拣并解除手机绑定，当有未分拣的明细时，仅解除手机绑定
        /// </summary>
        /// <param name="BatchNO"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage EndPFTBatch(string BatchNO)
        {
            bool _result=false;
            bool IsFinished = NewInstance.CheckBatchIsFinished(BatchNO);
            if (IsFinished)//分拣完成
            {
                _result = NewInstance.FinishBatch(BatchNO);
            }
            else
            {
                _result = NewInstance.DebindBatchPHONE(BatchNO);
            }
            if (_result)
            {
                return ResultsExtensions.GetSuccessJson(true);
            }
            else
            {
                return ResultsExtensions.GetFailJson(Setting.Instance.getMessageByKey("P0007"));
            }
        }
    }
}
